Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ----------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts -------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 303)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.94295 12.94361 12.94428 12.94497 12.94565 12.94634 12.94702 12.94769
##   [9] 12.94835 12.94898 12.94959 12.95017 12.95072 12.95123 12.95170 12.95211
##  [17] 12.95248 12.95278 12.95302 12.95319 12.95330 12.95332 12.95326 12.95311
##  [25] 12.95288 12.95254 12.95210 12.95156 12.95091 12.95014 12.94925 12.94827
##  [33] 12.94718 12.94601 12.94475 12.94341 12.94200 12.94054 12.93901 12.93744
##  [41] 12.93583 12.93418 12.93251 12.93081 12.92910 12.92739 12.92568 12.92397
##  [49] 12.92229 12.92062 12.91898 12.91738 12.91583 12.91432 12.91287 12.91149
##  [57] 12.91018 12.90883 12.90733 12.90570 12.90394 12.90207 12.90010 12.89804
##  [65] 12.89590 12.89370 12.89143 12.88913 12.88679 12.88444 12.88207 12.87970
##  [73] 12.87735 12.87503 12.87274 12.87050 12.86831 12.86620 12.86417 12.86224
##  [81] 12.86041 12.85870 12.85712 12.85567 12.85438 12.85265 12.84995 12.84634
##  [89] 12.84191 12.83673 12.83089 12.82446 12.81751 12.81013 12.80239 12.79438
##  [97] 12.78616 12.77781 12.76942 12.76106 12.75280 12.74474 12.73693 12.72947
## [105] 12.72242 12.71587 12.70989 12.70457 12.69997 12.69618 12.69327 12.69132
## [113] 12.69042 12.69000 12.68950 12.68895 12.68838 12.68783 12.68734 12.68692
## [121] 12.68663 12.68649 12.68654 12.68682 12.68735 12.68817 12.68931 12.69081
## [129] 12.69271 12.69504 12.69782 12.70110 12.70491 12.70928 12.71426 12.72103
## [137] 12.73051 12.74231 12.75600 12.77119 12.78748 12.80445 12.82170 12.83882
## [145] 12.85542 12.87109 12.88542 12.89800 12.90843 12.91886 12.93146 12.94586
## [153] 12.96170 12.97862 12.99625 13.01424 13.03222 13.04982 13.06669 13.08245
## [161] 13.09675 13.10923 13.11951 13.13024 13.14400 13.16043 13.17912 13.19970
## [169] 13.22176 13.24494 13.26883 13.29305 13.31721 13.34092 13.36380 13.38546
## [177] 13.40551 13.42356 13.43923 13.45212 13.46185 13.46803 13.47222 13.47621
## [185] 13.47994 13.48337 13.48645 13.48913 13.49136 13.49309 13.49428 13.49488
## [193] 13.49483 13.49409 13.49261 13.49034 13.48723 13.48324 13.47831 13.47240
## [201] 13.46545 13.45743 13.44827 13.43794 13.42638 13.41263 13.39598 13.37682
## [209] 13.35551 13.33243 13.30794 13.28241 13.25621 13.22972 13.20330 13.17732
## [217] 13.15216 13.12818 13.10575 13.08135 13.05177 13.01781 12.98028 12.94001
## [225] 12.89779 12.85446 12.81080 12.76765 12.72581 12.68610 12.64933 12.61630
## [233] 12.58784 12.56052 12.53070 12.49890 12.46563 12.43141 12.39676 12.36219
## [241] 12.32821 12.29534 12.26410 12.23500 12.20855 12.18527 12.16568 12.14800
## [249] 12.13022 12.11246 12.09486 12.07756 12.06070 12.04441 12.02882 12.01407
## [257] 12.00030 11.98764 11.97623 11.96621 11.95770 11.95017 11.94302 11.93631
## [265] 11.93010 11.92447 11.91948 11.91518 11.91164 11.90893 11.90711 11.90625
## [273] 11.90640 11.90763 11.91001 11.91334 11.91737 11.92212 11.92762 11.93388
## [281] 11.94091 11.94874 11.95738 11.96686 11.97718 11.98836 12.00043 12.01339
## [289] 12.02727 12.04218 12.05818 12.07524 12.09332 12.11240 12.13243 12.15337
## [297] 12.17520 12.19789 12.22138 12.24566 12.27068 12.29641 12.32282
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 303)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59145 12.58855 12.58575 12.58303 12.58040 12.57785 12.57538 12.57299
##   [9] 12.57067 12.56841 12.56622 12.56409 12.56202 12.56000 12.55804 12.55611
##  [17] 12.55424 12.55240 12.55059 12.54882 12.54708 12.54537 12.54367 12.54199
##  [25] 12.54033 12.53868 12.53704 12.53540 12.53376 12.53211 12.53043 12.52874
##  [33] 12.52705 12.52535 12.52365 12.52196 12.52029 12.51864 12.51702 12.51543
##  [41] 12.51388 12.51237 12.51092 12.50952 12.50819 12.50693 12.50574 12.50463
##  [49] 12.50362 12.50269 12.50187 12.50115 12.50054 12.50005 12.49969 12.49945
##  [57] 12.49935 12.49932 12.49928 12.49924 12.49921 12.49917 12.49915 12.49913
##  [65] 12.49913 12.49915 12.49919 12.49925 12.49933 12.49945 12.49960 12.49979
##  [73] 12.50001 12.50028 12.50059 12.50095 12.50137 12.50184 12.50236 12.50295
##  [81] 12.50361 12.50433 12.50512 12.50598 12.50693 12.50795 12.50906 12.51025
##  [89] 12.51153 12.51291 12.51438 12.51595 12.51763 12.51941 12.52130 12.52330
##  [97] 12.52541 12.52765 12.53000 12.53248 12.53509 12.53783 12.54070 12.54371
## [105] 12.54686 12.55015 12.55359 12.55718 12.56092 12.56482 12.56888 12.57310
## [113] 12.57748 12.58173 12.58557 12.58905 12.59223 12.59516 12.59788 12.60046
## [121] 12.60294 12.60537 12.60781 12.61031 12.61292 12.61569 12.61868 12.62193
## [129] 12.62550 12.62944 12.63380 12.63864 12.64400 12.64994 12.65650 12.66502
## [137] 12.67648 12.69044 12.70644 12.72405 12.74282 12.76230 12.78204 12.80161
## [145] 12.82055 12.83842 12.85477 12.86916 12.88114 12.89273 12.90615 12.92128
## [153] 12.93797 12.95610 12.97552 12.99610 13.01770 13.04020 13.06345 13.08732
## [161] 13.11167 13.13637 13.16128 13.18627 13.21121 13.23595 13.26036 13.28431
## [169] 13.30766 13.33028 13.35203 13.37277 13.39237 13.41070 13.42762 13.44299
## [177] 13.45668 13.46856 13.47848 13.48632 13.49193 13.49519 13.49661 13.49683
## [185] 13.49589 13.49383 13.49070 13.48652 13.48135 13.47521 13.46816 13.46023
## [193] 13.45146 13.44190 13.43157 13.42053 13.40882 13.39646 13.38351 13.37000
## [201] 13.35598 13.34148 13.32654 13.31121 13.29553 13.27741 13.25516 13.22941
## [209] 13.20077 13.16986 13.13729 13.10369 13.06966 13.03584 13.00283 12.97125
## [217] 12.94172 12.91487 12.89129 12.86859 12.84406 12.81785 12.79015 12.76112
## [225] 12.73092 12.69973 12.66772 12.63504 12.60188 12.56840 12.53476 12.50114
## [233] 12.46771 12.43462 12.40206 12.37019 12.33917 12.30918 12.28039 12.25296
## [241] 12.22705 12.20285 12.18052 12.16022 12.14212 12.12640 12.11322 12.10159
## [249] 12.09041 12.07971 12.06950 12.05980 12.05063 12.04200 12.03393 12.02643
## [257] 12.01953 12.01325 12.00759 12.00257 11.99822 11.99455 11.99158 11.98932
## [265] 11.98779 11.98701 11.98700 11.98777 11.98934 11.99173 11.99495 11.99903
## [273] 12.00397 12.00980 12.01653 12.02411 12.03246 12.04158 12.05147 12.06214
## [281] 12.07357 12.08577 12.09873 12.11247 12.12696 12.14222 12.15824 12.17501
## [289] 12.19255 12.21084 12.22989 12.24970 12.27026 12.29157 12.31363 12.33645
## [297] 12.36001 12.38432 12.40937 12.43517 12.46171 12.48900 12.51703
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 303)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.88916 11.89070 11.89228 11.89388 11.89550 11.89713 11.89877 11.90040
##   [9] 11.90202 11.90362 11.90520 11.90674 11.90825 11.90970 11.91110 11.91244
##  [17] 11.91371 11.91490 11.91601 11.91702 11.91794 11.91875 11.91944 11.92002
##  [25] 11.92046 11.92077 11.92094 11.92096 11.92082 11.92051 11.92003 11.91937
##  [33] 11.91852 11.91748 11.91624 11.91478 11.91309 11.91115 11.90897 11.90658
##  [41] 11.90399 11.90122 11.89829 11.89522 11.89203 11.88873 11.88535 11.88189
##  [49] 11.87839 11.87485 11.87129 11.86775 11.86422 11.86074 11.85731 11.85396
##  [57] 11.85070 11.84756 11.84455 11.84169 11.83900 11.83650 11.83420 11.83212
##  [65] 11.82975 11.82659 11.82270 11.81816 11.81301 11.80734 11.80120 11.79465
##  [73] 11.78777 11.78060 11.77323 11.76570 11.75809 11.75045 11.74286 11.73538
##  [81] 11.72806 11.72098 11.71419 11.70777 11.70177 11.69625 11.69129 11.68695
##  [89] 11.68329 11.68037 11.67825 11.67701 11.67569 11.67336 11.67012 11.66607
##  [97] 11.66130 11.65592 11.65003 11.64372 11.63710 11.63025 11.62329 11.61631
## [105] 11.60941 11.60269 11.59624 11.59017 11.58457 11.57955 11.57520 11.57163
## [113] 11.56892 11.56718 11.56651 11.56701 11.56877 11.57190 11.57650 11.58265
## [121] 11.59114 11.60243 11.61617 11.63199 11.64956 11.66852 11.68851 11.70918
## [129] 11.73018 11.75116 11.77176 11.79164 11.81043 11.82778 11.84335 11.86035
## [137] 11.88179 11.90698 11.93523 11.96586 11.99818 12.03151 12.06516 12.09845
## [145] 12.13069 12.16119 12.18927 12.21425 12.23543 12.25664 12.28171 12.30998
## [153] 12.34082 12.37355 12.40753 12.44212 12.47665 12.51047 12.54293 12.57338
## [161] 12.60117 12.62564 12.64615 12.66511 12.68524 12.70635 12.72822 12.75065
## [169] 12.77344 12.79638 12.81926 12.84188 12.86404 12.88552 12.90613 12.92565
## [177] 12.94389 12.96063 12.97567 12.98881 12.99984 13.00856 13.01607 13.02358
## [185] 13.03099 13.03822 13.04518 13.05179 13.05795 13.06358 13.06859 13.07290
## [193] 13.07641 13.07904 13.08071 13.08132 13.08079 13.07903 13.07596 13.07148
## [201] 13.06551 13.05796 13.04874 13.03778 13.02497 13.00882 12.98830 12.96400
## [209] 12.93655 12.90653 12.87457 12.84127 12.80724 12.77308 12.73940 12.70681
## [217] 12.67592 12.64733 12.62165 12.59391 12.55947 12.51943 12.47489 12.42698
## [225] 12.37679 12.32544 12.27402 12.22365 12.17544 12.13048 12.08990 12.05479
## [233] 12.02627 12.00075 11.97414 11.94673 11.91884 11.89076 11.86282 11.83530
## [241] 11.80852 11.78278 11.75840 11.73567 11.71490 11.69640 11.68047 11.66605
## [249] 11.65191 11.63813 11.62479 11.61195 11.59969 11.58810 11.57723 11.56717
## [257] 11.55800 11.54978 11.54259 11.53651 11.53160 11.52743 11.52354 11.52002
## [265] 11.51695 11.51443 11.51253 11.51135 11.51098 11.51149 11.51298 11.51553
## [273] 11.51924 11.52418 11.53044 11.53776 11.54582 11.55465 11.56427 11.57469
## [281] 11.58594 11.59805 11.61103 11.62490 11.63968 11.65541 11.67209 11.68975
## [289] 11.70840 11.72817 11.74912 11.77120 11.79438 11.81861 11.84387 11.87010
## [297] 11.89728 11.92535 11.95429 11.98404 12.01458 12.04586 12.07784
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")